package com.aispeech.astools.view;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aispeech.astools.R;

import java.util.ArrayList;
import java.util.List;

import androidx.cardview.widget.CardView;
import androidx.viewpager.widget.PagerAdapter;

public class CardPagerAdapter extends PagerAdapter implements CardAdapter {
    private final String TAG = "CardPagerAdapter";

    private List<CardView> mViews;
    private List<CardItem> mData;
    private float mBaseElevation;

    public CardPagerAdapter() {
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void addCardItem(CardItem item) {
        mViews.add(null);
        mData.add(item);
    }

    public void removeCardItem(String id) {
        for (int i = 0; i < mData.size(); ++i) {
            CardItem ci = mData.get(i);
            if (id.equals(ci.getDevId())) {
                Log.d(TAG, "removeCardItem: ");
                boolean remove = mData.remove(ci);
                Log.d(TAG, "removeCardItem() remove = [" + remove + "]");
                mViews.remove(i);
            }
        }
    }

    public void cleanCardItem() {
        Log.d(TAG, "cleanCardItem: ");
        mData.clear();
        mViews.clear();
    }

    @Override
    public int getItemPosition(Object object) {
//        return super.getItemPosition(object);
        // 最简单解决 notifyDataSetChanged() 页面不刷新问题的方法
        return POSITION_NONE;
    }

    public float getBaseElevation() {
        return mBaseElevation;
    }

    @Override
    public CardView getCardViewAt(int position) {
        Log.d(TAG, "getCardViewAt() called with: position = [" + position + "]");
        if (mViews.size() < 1) {
            return null;
        }
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        Log.d(TAG, "getCount() called");
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.d(TAG, "instantiateItem() called with: position = [" + position + "]");
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.adapter, container, false);
        container.addView(view);
        bind(mData.get(position), view);
        CardView cardView = view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        Log.d(TAG, "destroyItem() called with: position = [" + position + "]");
//        mViews.set(position, null);
    }

    private void bind(CardItem item, View view) {
        TextView tvName = view.findViewById(R.id.tv_remote_name);
        TextView tvId = view.findViewById(R.id.tv_remote_id);

        tvName.setText(item.getDevName());
        tvId.setText(item.getDevId());
    }

}
